/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.OlahUser;
import entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author RISKHA
 */
public class LogikaShow {

    HttpServletRequest request;

    public LogikaShow(HttpServletRequest request) {
        this.request = request;
    }

    public String editData() {
        String username = request.getParameter("username");
        User user = new User();
        OlahUser ou = new OlahUser();
        user = ou.getUser(username);
        request.setAttribute("user", user);
        return "editdata.jsp";
    }

    public String updateData() throws Exception {
        User user = new User();
        OlahUser ou = new OlahUser();
        List<User> users = new ArrayList<User>();

        String username = request.getParameter("userName");
        String password = request.getParameter("pass");
        String kadarGula = request.getParameter("kadarGula");
        String beratBadan = request.getParameter("beratBadan");

        user = ou.getUser(username);
        user.setUsername(username);
        user.setPass(password);
        user.setKadargula(kadarGula);
        user.setBeratbadan(beratBadan);
        ou.editUser(user);

        users = ou.getUsers();
        request.setAttribute("users", user);
        return "show.jsp";
    }

    public String delete() throws NonexistentEntityException {
        String idUser = request.getParameter("idUser");
        List<User> user = new ArrayList<User>();
        OlahUser ou = new OlahUser();

        if (idUser == null) {
            user = ou.getUsers();
            request.setAttribute("users", user);
        } else {
            int id = Integer.parseInt(idUser);
            ou.deleteUser(id);
            user = ou.getUsers();
            request.setAttribute("users", user);
        }
        return "show.jsp";
    }
}
